package com.travelsmart.server.integration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.smart.hotelsmart.BookHotelRequestType;
import com.smart.hotelsmart.BookHotelResponseType;
import com.smart.hotelsmart.HotelService;
import com.smart.hotelsmart.InfoBookHotelRequestType;
import com.smart.hotelsmart.InfoBookHotelResponseType;
import com.smart.hotelsmart.ListBookHotelRequestType;
import com.smart.hotelsmart.ListBookHotelResponseType;
import com.smart.hotelsmart.SummaryBookHotelType;
/*/# INICIO - EXERC. 1

// FIM - EXERC. 1 #*/
import com.smart.hotelsmart.InfoHotelRequestType;
import com.smart.hotelsmart.InfoHotelResponseType;

import com.smart.hotelsmart.SearchHotelRequestType;
import com.smart.hotelsmart.SearchHotelResponseType;

import com.smart.hotelsmart.SummaryHotelType;
import com.travelsmart.client.bean.to.FilterInfoHotel;
import com.travelsmart.client.bean.to.FilterPesquisaHotel;
import com.travelsmart.client.bean.to.HotelTO;
import com.travelsmart.client.bean.to.ReservaHotelTO;
import com.travelsmart.server.util.UtilDate;

public class HotelSmartClient {

	private static HotelSmartClient instance = new HotelSmartClient();

	public static HotelSmartClient getInstance() {
		return instance;
	}

	public HotelTO infoHotel(FilterInfoHotel filter) {
		InfoHotelRequestType request = new InfoHotelRequestType();
		request.setLinkSelection(filter.getLinkSelection());
		request.setCodeHotel(filter.getCodeHotel());
		request.setDepartureDate(UtilDate.criarGregorianCalendar(filter
				.getDepartureDate()));
		request.setArrivalDate(UtilDate.criarGregorianCalendar(filter
				.getArrivalDate()));
		InfoHotelResponseType response = getInfoHotel(request);
		HotelTO to = new BuilderHotelTO().criarHotelTO(response);
		return to;
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public List<HotelTO> pesquisarHoteisDisponiveis(FilterPesquisaHotel filter) {
		List<HotelTO> retorno = null;
		try {
			SearchHotelRequestType request = new SearchHotelRequestType();
			request.setDestination(filter.getLocal());
			request.setArrival(UtilDate.criarGregorianCalendar(filter
					.getEntrada()));
			request.setDeparture(UtilDate.criarGregorianCalendar(filter
					.getSaida()));
			SearchHotelResponseType response = getHoteisDisponiveis(request);
			retorno = criaListHotelTO(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}

	private List<HotelTO> criaListHotelTO(SearchHotelResponseType response) {
		assert response != null;
		List<HotelTO> retorno = new ArrayList<HotelTO>();
		List<SummaryHotelType> hoteis = response.getSummaryHotel();
		for (SummaryHotelType summary : hoteis) {
			HotelTO to = new HotelTO();
			to.setDestinationId(summary.getDestinationId());
			to.setCodigo(summary.getCodeHotel());
			to.setLinkImage(summary.getLinkImage());
			to.setLinkSelection(summary.getLinkSelection());
			to.setNome(summary.getNameHotel());
			to.setPrecoMedio(summary.getAveragePrice());
			to.setQualidade(summary.getQuality());
			retorno.add(to);
		}
		return retorno;

	}

	public void bookHotel(ReservaHotelTO reserva, String username) {
		assert reserva != null;
		BookHotelRequestType request = new BuilderBookHotelRequestType()
				.criarListBookRoom(reserva, username);
		setBookHotel(request);
	}

	private static SearchHotelResponseType getHoteisDisponiveis(
			com.smart.hotelsmart.SearchHotelRequestType body) {
		com.smart.hotelsmart.HotelService service = new com.smart.hotelsmart.HotelService();
		com.smart.hotelsmart.HotelPortType port = service
				.getHotelPortTypePort();
		return port.getSearchAvailableHotels(body);
	}

	private static InfoHotelResponseType getInfoHotel(
			com.smart.hotelsmart.InfoHotelRequestType body) {
		com.smart.hotelsmart.HotelService service = new com.smart.hotelsmart.HotelService();
		com.smart.hotelsmart.HotelPortType port = service
				.getHotelPortTypePort();
		return port.getInfoHotel(body);
	}

	private static BookHotelResponseType setBookHotel(
			com.smart.hotelsmart.BookHotelRequestType body) {

		com.smart.hotelsmart.HotelService service = new com.smart.hotelsmart.HotelService();
		com.smart.hotelsmart.HotelPortType port = service
				.getHotelPortTypePort();
		return port.setBookHotel(body);
	}
	
	public ReservaHotelTO getInfoBookHotel(long code){
		InfoBookHotelRequestType request = new InfoBookHotelRequestType();
		request.setCodeBookHotel(code);
		InfoBookHotelResponseType response = getInfoBookHotel(request);
		ReservaHotelTO reserva = new BuilderReservaHotelTO().criarReservaHotelTo(response);
		return reserva;
	}
	
	private static ListBookHotelResponseType listBookHotel(ListBookHotelRequestType body){
		
		com.smart.hotelsmart.HotelService service = new com.smart.hotelsmart.HotelService();
		com.smart.hotelsmart.HotelPortType port = service.getHotelPortTypePort();
		
		return port.getListBookHotel(body);
	}
	
	private static InfoBookHotelResponseType getInfoBookHotel ( final InfoBookHotelRequestType body){
		
		com.smart.hotelsmart.HotelService service = new com.smart.hotelsmart.HotelService();
		
		com.smart.hotelsmart.HotelPortType port = service.getHotelPortTypePort();
		
		return port.getInfoBookHotel(body);
		
	}

	
	public List<ReservaHotelTO> listReserva(String username, int numeMax, int offset) {
		 List<ReservaHotelTO> retorno = null;
		 ListBookHotelRequestType request = new ListBookHotelRequestType ();
		 request.setCustomer(username);
		 request.setNumMax(numeMax);
		 request.setOffset(offset);
		 ListBookHotelResponseType response =  listBookHotel(request);
		 retorno = criarListReservaHotelTO(response);
		 return retorno;
		
	}

	private List<ReservaHotelTO> criarListReservaHotelTO(
			ListBookHotelResponseType response) {
		List<SummaryBookHotelType> list = response.getSummaryBookHotel();
		List<ReservaHotelTO> resp = new ArrayList<ReservaHotelTO> ();
		if (!list.isEmpty()){
			for (SummaryBookHotelType summaryBookHotelType : list) {
				ReservaHotelTO e = new ReservaHotelTO();
				HotelTO hotel = new HotelTO();
				hotel.setNome(summaryBookHotelType.getNomeHotel());
				e.setHotel(hotel);
				e.setDataEntrada(UtilDate.getDate(summaryBookHotelType.getArrival()));
				e.setDataSaida(UtilDate.getDate(summaryBookHotelType.getDeparture()));
				e.setData(new Date());
				e.setId(summaryBookHotelType.getCodeBookHotel());
				resp.add(e);
			}
		}
		return resp;
	}
	
	
	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/	
	
	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/
	
	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/
	
	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/
	
	/*/# INICIO - EXERC. 1
	
	// FIM - EXERC. 1 #*/

}
